#include "Finviz.h"

CandlestickChartData Finviz::CandlestickChartAnalyze(yyjson_val *root)
{
    CandlestickChartData data;

    yyjson_val *symbal = yyjson_obj_get(root, "ticker");
    yyjson_val *dataId = yyjson_obj_get(root, "dataId");
    yyjson_val *last_price = yyjson_obj_get(root, "lastClose");

    std::vector<float> open_arr = get_item_data<float>(yyjson_obj_get(root, "open"));
    std::vector<float> close_arr = get_item_data<float>(yyjson_obj_get(root, "close"));
    std::vector<float> high_arr = get_item_data<float>(yyjson_obj_get(root, "high"));
    std::vector<float> low_arr = get_item_data<float>(yyjson_obj_get(root, "low"));
    std::vector<uint64_t> date_arr = get_item_data<uint64_t>(yyjson_obj_get(root, "date"));
    std::vector<uint64_t> volume_arr = get_item_data<uint64_t>(yyjson_obj_get(root, "volume"));

    for (size_t i = 0; i < open_arr.size(); i++)
    {
        data.Items.push_back(CandlestickChartItem{
            open_arr.at(i),
            close_arr.at(i),
            low_arr.at(i),
            high_arr.at(i),
            volume_arr.at(i),
            date_arr.at(i)});
    }

    std::string data_id_str = yyjson_get_str(dataId);
    std::string price_str = data_id_str.substr(data_id_str.find('|') + 1);
    data.symbol = yyjson_get_str(symbal);
    data.last_price = std::stod(price_str);

    return data;
}
